<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Db;

class NoticeIndex extends Common
{
    //---------------------------------------------后端---------------------------------------------------------------
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['desc']) && !empty($post['desc'])){
            $where[] = ['text', 'like', '%'.$post['desc'].'%'];
        }
        $list = Db::name('notice_index')
            ->order('ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    public function setAdd($data)
    {
        return Db::name('notice')->save([
            'title' => trim($data['title']),
            'text'  => $data['text'],
            'ctime' => getUtcTime()
        ]);
    }

    public function setEdit($data)
    {
        $set_id         = $data['set_id'];
        $update_param   = [
            'title'     => trim($data['title'] ?? ''),
            'text'      => $data['text'],
            'ctime'     => $data['ctime']
        ];
        return Db::name('notice')
                ->where(['id'=>$set_id])
                ->update($update_param);
    }
}
